import {
  Message
} from 'element-ui'
const tagsView = {
  state: {
    /**
     * ymc 默认添加一个首页标签
     */
    visitedViews: [{
      name: "dashboard",
      path: "/dashboard",
      title: "dashboard",
      query:'',
    }],
    cachedViews: [],
    activeRouteName: '',//选中菜单名称
  },
  mutations: {
    ADD_VISITED_VIEWS: (state, view) => {
      if (state.visitedViews.some(v => v.path === view.path)) return
      state.visitedViews.push({
        name: view.name,
        path: view.path,
        query: createQuery(view),
        title: view.meta.title || 'no-name'
      })
      if (!view.meta.noCache) {
        state.cachedViews.push(view.name)
      }
    },
    DEL_VISITED_VIEWS: (state, view) => {
      if (state.visitedViews.length == 1) {
        Message({
          message: '最后一个tab不能删除',
          type: 'error',
          duration: 5 * 1000
        })
        return;
      }
      for (const [i, v] of state.visitedViews.entries()) {
        if (v.path === view.path) {
          state.visitedViews.splice(i, 1)
          break
        }
      }
      for (const i of state.cachedViews) {
        if (i === view.name) {
          const index = state.cachedViews.indexOf(i)
          state.cachedViews.splice(index, 1)
          break
        }
      }
    },
    DEL_OTHERS_VIEWS: (state, view) => {
      let db = state.visitedViews[0];
      for (const [i, v] of state.visitedViews.entries()) {
        if (v.path === view.path) {
          state.visitedViews = state.visitedViews.slice(i, i + 1)
          break
        }
      }
      for (const i of state.cachedViews) {
        if (i === view.name) {
          const index = state.cachedViews.indexOf(i)
          state.cachedViews = state.cachedViews.slice(index, i + 1)
          break
        }
      }
      state.visitedViews = [db, ...state.visitedViews];//保留首页
    },
    DEL_ALL_VIEWS: (state) => {
      if (state.visitedViews.length == 1) {
        Message({
          message: '最后一个tab不能删除',
          type: 'error',
          duration: 5 * 1000
        })
        return;
      }
      let visitedViews = state.visitedViews[0];
      let cachedViews = state.cachedViews[0];
      state.visitedViews = []
      state.cachedViews = []
      state.visitedViews.push(visitedViews);
      state.cachedViews.push(cachedViews);
    },
    SET_ACTIVE_ROUTER_NAME: (state, view) => {
      state.activeRouteName = view;
    }
  },
  actions: {
    setActiveRouterName ({ commit }, view) {
      commit('SET_ACTIVE_ROUTER_NAME', view)
    },
    addVisitedViews ({
      commit
    }, view) {
      commit('ADD_VISITED_VIEWS', view)
    },
    delVisitedViews ({
      commit,
      state
    }, view) {
      return new Promise((resolve) => {
        commit('DEL_VISITED_VIEWS', view)
        resolve([...state.visitedViews])
      })
    },
    delOthersViews ({
      commit,
      state
    }, view) {
      return new Promise((resolve) => {
        commit('DEL_OTHERS_VIEWS', view)
        resolve([...state.visitedViews])
      })
    },
    delAllViews ({
      commit,
      state
    }) {
      return new Promise((resolve) => {
        commit('DEL_ALL_VIEWS')
        resolve([...state.visitedViews])
      })
    }
  }
}
//
function createQuery (view) {
  let { query } = view;
  let path = `?`
  let queryArry = Object.keys(query);
  if (queryArry.length > 0) {
    queryArry.map(item => {
      path += `${item}=${query[item]}&`
    })
    //TODO 如果quey参数中有对象，不能正确解析，待处理
  }
  return path;
}
export default tagsView
